//
//  RBGEmailController.m
//  BateauGenerique
//
//  Created by Florent Champigny on 29/06/2014.
//  Copyright (c) 2014 Kevin De Jesus Ferreira. All rights reserved.
//

#import "RBGEmailController.h"
#import "REPagedScrollView.h"
#import "UIImageView+WebCache.h"
#import "RBGServiceAnnonce.h"
#import "MWPhotoBrowser.h"
#import <MapKit/MapKit.h>
#import "RBGFavorisManager.h"
#import "RBGColorManager.h"
#import "RBGAppDelegate.h"
#import <MessageUI/MFMailComposeViewController.h>
#import "RBGSelectorValue.h"
#import "Departement.h"
#import "RBGServiceChargement.h"
#import "RBGServiceAnnonce.h"

@interface RBGEmailController ()<RBGSelectorValueDelegate,MFMailComposeViewControllerDelegate,RBGServiceChargementDelegate,RBGServiceAnnonceDelegate,UIAlertViewDelegate>{
    RBGAppDelegate *appDelegate;
    NSMutableArray * departements;
    NSString * departement_id;
}

@end

@implementation RBGEmailController


- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self.description setText:self.message];
    
    appDelegate = (RBGAppDelegate *)[[UIApplication sharedApplication] delegate];
    
    departements = [[NSMutableArray alloc]init];
    UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc] initWithTitle:@"Envoyer" style:UIBarButtonItemStylePlain target:self action:@selector(envoyerEmail:)];
    self.navigationItem.rightBarButtonItem = anotherButton;
    
    
    
}


- (IBAction)envoyerEmail:(id)sender {
    NSLog(@"Envoyer email");
    
    if(self.email.text.length <= 0){
        [self afficherAlerte:@"Veuillez remplir votre email"];
        
    }else{
        
        if(self.nom.text.length <= 0){
            [self afficherAlerte:@"Veuillez remplir votre nom"];
            
        }else{
            
            if(self.description.text.length <= 0){
                [self afficherAlerte:@"Veuillez remplir un message"];
                
            }else{
                NSMutableDictionary * arguments = [[NSMutableDictionary alloc]init];
                
                [arguments addEntriesFromDictionary:@{@"nom":self.nom.text}];
                
                [arguments addEntriesFromDictionary:@{@"email":self.email.text}];
                
                [arguments addEntriesFromDictionary:@{@"mesg":self.description.text}];
                
                if(self.idad != nil)
                    [arguments addEntriesFromDictionary:@{@"idad":self.idad}];
                if(self.type != nil)
                    
                    [arguments addEntriesFromDictionary:@{@"type":self.type}];
                
                if(self.numero != nil)
                    [arguments addEntriesFromDictionary:@{@"tel1":self.numero.text}];
                
                if(departement_id != nil)
                    [arguments addEntriesFromDictionary:@{@"iddept":departement_id}];
                
                
                if([self.clidirect isEqualToString:@"1"])
                    [arguments addEntriesFromDictionary:@{@"clidirect":@"1"}];
                
                RBGServiceAnnonce * serviceEmail = [[RBGServiceAnnonce alloc]init];
                serviceEmail.delegate = self;
                [serviceEmail envoieEmail:arguments];
                /*
                 MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
                 [mc setSubject:self.titre];
                 mc.mailComposeDelegate = self;
                 [mc setMessageBody:[NSString stringWithFormat:@"%@\nNom : %@\nDépartement : %@\nNuméro : %@",self.description.text,self.nom.text,self.departement.text,self.numero.text] isHTML:NO];
                 [mc setToRecipients:self.toRecipents];
                 
                 // Present mail view controller on screen
                 [self presentViewController:mc animated:YES completion:NULL];
                 
                 */}
        }
        
    }
}

-(void) afficherAlerte:(NSString *)mes
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Erreur Champs"
                                                    message:mes
                                                   delegate:nil
                                          cancelButtonTitle:@"OK"
                                          otherButtonTitles:nil];
    [alert show];
}

- (IBAction)changeClearColor:(UIButton *)sender
{
    [sender setBackgroundColor:[UIColor clearColor]];
    
}
- (IBAction)changeColor:(id)sender{
    
    [sender setBackgroundColor:appDelegate.donnees.parametres.backgroundColorDeux];
}

- (IBAction)demanderValue:(UIButton *)sender {
    
    [self changeClearColor:sender];
    
    if(departements == nil || ([departements count
                                ]<= 0)){
        [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
        
        RBGServiceChargement * chargement = [[RBGServiceChargement alloc]init];
        chargement.delegate = self;
        [chargement chargerDepartements];
    }else{
        RBGSelectorValue * selectorValue = [[RBGSelectorValue alloc]init];
        selectorValue.type = 0;
        selectorValue.delegate = self;
        selectorValue.values = [departements mutableCopy];
        selectorValue.facultatif = YES;
        [self.navigationController pushViewController:selectorValue animated:YES];
    }
    
    
}

- (void) receiveValueSelected:(int)valueSelected
                andTypeValeur:(int)type{
    
    if(valueSelected != -1){
        Departement * departement = [appDelegate.donnees.departements objectAtIndex:valueSelected];
        [self.valueDepartement setText:departement.nom];
        departement_id = departement.id;
    }else{
        [self.valueDepartement setText:@"Indifférent"];
        
    }
}

- (void) receiveDonneesChargement:(id)donnees
                         andType :(int)typeDonnees{
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
    
    appDelegate.donnees.departements = (NSArray *) donnees;
    
    for(Departement *d in appDelegate.donnees.departements){
        [departements addObject:d.nom];
    }
    
    RBGSelectorValue * selectorValue = [[RBGSelectorValue alloc]init];
    selectorValue.type = 0;
    selectorValue.delegate = self;
    selectorValue.values = [departements mutableCopy];
    selectorValue.facultatif = YES;
    [self.navigationController pushViewController:selectorValue animated:YES];
    
}

- (void) receiveErreurChargement:(int)statut{
    UIAlertView *alert = [[UIAlertView alloc]
                          
                          initWithTitle:@"Erreur"
                          message:@"Erreur réseau : votre téléphone ne peut pas accéder aux informations"
                          delegate:nil
                          cancelButtonTitle:@"Ok"
                          otherButtonTitles:nil];
    
    [alert show];
}

- (void) receiveDonneesAnnonce:(id)donnees
                      andType :(int)typeDonnees{
    UIAlertView *alert = [[UIAlertView alloc]
                          
                          initWithTitle:@"Email"
                          message:@"Email envoyé"
                          delegate:self
                          cancelButtonTitle:@"Ok"
                          otherButtonTitles:nil];
    
    [alert show];
}

- (void) receiveErreurAnnonce:(int)statut{
    UIAlertView *alert = [[UIAlertView alloc]
                          
                          initWithTitle:@"Erreur"
                          message:@"Erreur réseau : votre téléphone ne peut pas accéder aux informations"
                          delegate:nil
                          cancelButtonTitle:@"Ok"
                          otherButtonTitles:nil];
    
    [alert show];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - MFMailComposeViewControllerDelegate
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error{
    [self dismissViewControllerAnimated:YES completion:nil];
}
@end
